/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.goods.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.goods.entity.ZtComment;
import com.jeesite.modules.goods.service.ZtCommentService;

/**
 * 测试数据Controller
 * 
 * @author ThinkGem
 * @version 2018-04-22
 */
@Controller
@RequestMapping(value = "${adminPath}/ztComment")
public class ZtCommentController extends BaseController {

	@Autowired
	private ZtCommentService ztCommentService;

	/**
	 * 获取数据
	 */
	@ModelAttribute
	public ZtComment get(ZtComment ztComment, boolean isNewRecord) {
		ztComment=ztCommentService.get(ztComment);
		 
		return ztComment;
	}

	/**
	 * 查询列表
	 */
	@RequiresPermissions("ztGoods:ztGoodsData:view")
	@RequestMapping(value = { "list", "" })
	public String list(ZtComment ztComment, Model model) {
		model.addAttribute("ztComment", ztComment);
		return "modules/ztGoods/ztCommentList";
	}

	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("ztGoods:ztGoodsData:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<ZtComment> listData(ZtComment ztComment, HttpServletRequest request,
			HttpServletResponse response) {
		Page<ZtComment> page = ztCommentService.findPage(new Page<ZtComment>(request,
				response), ztComment);
		for(ZtComment zc:page.getList()){
			String imgs=zc.getImgs();
			if(StringUtils.isNotBlank(imgs)){
			JSONArray ja=JSONArray.parseArray(imgs);
			String arr="";
			for(int i=0;i<ja.size();i++){
				String s=ja.get(i).toString();
				arr+=s+",";
			}
			zc.setImgs(arr.substring(0,arr.length()-1));
			}
		}
		return page;
	}
	public static void main(String[] args) {
		JSONArray ja=JSONArray.parseArray("[\"https://wewoo.gzyuejiang.com/wewoo/userfiles/images/2019/1/7/219a0ffb-4534-404b-9612-a0fdf385ddd6.jpg\",\"https://wewoo.gzyuejiang.com/wewoo/userfiles/images/2019/1/7/219a0ffb-4534-404b-9612-a0fdf385ddd6.jpg\",\"https://wewoo.gzyuejiang.com/wewoo/userfiles/images/2019/1/7/b3a42343-4869-45c6-94a7-53afd70ef271.jpg\"]");
		String arr="";
		for(int i=0;i<ja.size();i++){
			String s=ja.get(i).toString();
			arr+=s+",";
		}
		System.out.println(arr.substring(0,arr.length()-1));
	}
	
	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("ztGoods:ztGoodsData:view")
	@RequestMapping(value = "form")
	public String form(ZtComment ztComment, Model model) {
		model.addAttribute("ztComment", ztComment);
		return "modules/ztGoods/ztCommentForm";
	}
	
	/**
	 * 保存数据
	 */
	@RequiresPermissions("ztGoods:ztGoodsData:view")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated ZtComment ztComment) {
		ztCommentService.save(ztComment);
		return renderResult(Global.TRUE, text("保存数据成功！"));
	}
	

	/**
	 * 删除数据
	 */
	@RequiresPermissions("ztGoods:ztGoodsData:view")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(ZtComment ztComment) {
		ztCommentService.delete(ztComment);
		return renderResult(Global.TRUE, text("删除评论成功！"));
	}
	
}